﻿using System;
using System.Collections.Generic;
using System.Linq;
using EncuentraloSilverlight.Models;

namespace EncuentraloSilverlight.Web.Models
{
    public partial class User
    {
        public bool IsValid
        {
            get { return (GetUserRuleViolations().Count() == 0); }
        }
        public IEnumerable<RuleViolation> GetUserRuleViolations()
        {
            if (String.IsNullOrEmpty(Name))
                yield return new RuleViolation("Username required", "Username");
            if (String.IsNullOrEmpty(Password))
                yield return new RuleViolation("Password required", "Password");
            if (String.IsNullOrEmpty(Mail))
                yield return new RuleViolation("Mail required", "Mail");
            else if (!ParseMail(Mail))
               yield return new RuleViolation("Mail not valid", "Mail");
            yield break;
        }

        public static bool ParseMail(string user)
        {
            int at_position = user.IndexOf('@');
            if (at_position == -1) return false;
            string nombre = user.Substring(0, at_position);

            string dominio = user.Substring(at_position + 1);
            int dot_position = user.IndexOf('.');
            if (dot_position == -1) return false;

            return true;
        }
    }
}